package com.runen.infinitemovement.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.ColumnAdapter;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.ActivityAPI;
import com.runen.infinitemovement.model.entity.CollageInfoEntity;
import com.runen.infinitemovement.widget.CustomLoadMoreView;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LongCollegeActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.top_btn_left)
    Button topBtnLeft;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.top_btn_right)
    Button topBtnRight;
    @BindView(R.id.top_tv_right)
    TextView topTvRight;
    @BindView(R.id.top_iv_right)
    ImageView topIvRight;
    @BindView(R.id.ll_title)
    LinearLayout llTitle;
    @BindView(R.id.rv_long_college_list)
    RecyclerView rvLongCollegeList;
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;
    private ColumnAdapter columnAdapter;
    private int page = 2;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_long_college;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        topTitle.setText("长征学院");
        srl.setOnRefreshListener(this);
        LinearLayoutManager layoutManager3 = new LinearLayoutManager(this);
        layoutManager3.setOrientation(LinearLayoutManager.VERTICAL);
        rvLongCollegeList.setLayoutManager(layoutManager3);
        columnAdapter = new ColumnAdapter(this);
        columnAdapter.getLoadMoreModule().setLoadMoreView(new CustomLoadMoreView());
        rvLongCollegeList.setAdapter(columnAdapter);
        columnAdapter.getLoadMoreModule().setAutoLoadMore(false);
        columnAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Intent intent = new Intent(LongCollegeActivity.this, ColumnDetailsActivity.class);
                intent.putExtra("informationId", columnAdapter.getData().get(position).informationId);
                startActivity(intent);
            }
        });

        initData();
        iniLoadMore();
    }

    private void iniLoadMore() {
        columnAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                loadMore();
            }
        });
        columnAdapter.getLoadMoreModule().setAutoLoadMore(true);
        columnAdapter.getLoadMoreModule().setEnableLoadMoreIfNotFullPage(false);
    }

    private void initData() {
        srl.setRefreshing(true);
        String string = "?limit=10&page=1";
        new ActivityAPI().getMyLongCollageInfo(string, new BaseResultCallback<CollageInfoEntity>() {
            @Override
            public void onResponse(CollageInfoEntity response) {
                srl.setRefreshing(false);
                if (response.code == 0) {
                    if (response.data != null) {
                        columnAdapter.setNewData(response.data.informationList);
                        if (columnAdapter.getData().size() >= response.data.count) {
                            columnAdapter.getLoadMoreModule().loadMoreEnd(true);
                            columnAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        }
                    }
                } else {
                    ToastUtils.showShort(response.msg);
                }

            }
        });
    }

    @OnClick(R.id.top_btn_left)
    public void onViewClicked() {
        finish();
    }


    @Override
    public void onRefresh() {
        page = 2;
        initData();
    }

    public void loadMore() {
        String string = "?limit=10&page=%s";
        new ActivityAPI().getMyLongCollageInfo(String.format(string, page), new BaseResultCallback<CollageInfoEntity>() {
            @Override
            public void onResponse(CollageInfoEntity response) {
                if (response.code == 0) {
                    if (response.data != null && response.data.informationList.size() > 0) {
                        columnAdapter.addData(response.data.informationList);
                        if (columnAdapter.getData().size() >= response.data.count) {
                            columnAdapter.getLoadMoreModule().loadMoreEnd(true);
                            columnAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        } else {
                            page++;
                            columnAdapter.getLoadMoreModule().loadMoreComplete();
                            columnAdapter.getLoadMoreModule().setEnableLoadMore(true);
                        }
                    }
                } else {
                    ToastUtils.showShort(response.msg);
                }

            }
        });
    }
}
